package org.hive.utils
{
	import flash.net.URLLoader;
	import flash.events.*;
    import flash.net.*;
    
    import org.hive.events.ServerXMLEvent;
	
	public class ServerXML extends EventDispatcher
	{
		private var __loader:URLLoader;
		private var __source:String;
		private var __xml:XML;
		
		public function get xml( ):XML
		{
			return __xml;
		}
		
		public function ServerXML( url:String )
		{	
			__source = url;
			__loader = new URLLoader( );
			
			__loader.addEventListener( Event.COMPLETE, completeHandler );
            __loader.addEventListener( Event.OPEN, openHandler );
            __loader.addEventListener( ProgressEvent.PROGRESS, progressHandler );
            __loader.addEventListener( SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler );
            __loader.addEventListener( HTTPStatusEvent.HTTP_STATUS, httpStatusHandler );
            __loader.addEventListener( IOErrorEvent.IO_ERROR, ioErrorHandler );
		}
		
		public function request( ):void
		{
			try {
                __loader.load( new URLRequest( __source )  );                
            } catch ( error:Error ) {
                trace("Unable to load requested document.");
            }
		}
		
		private function completeHandler(event:Event):void 
		{
    		__xml = XML( __loader.data );
			
			var serverXMLEvent:ServerXMLEvent = new ServerXMLEvent( ServerXMLEvent.ON_READY, __xml );
			dispatchEvent( serverXMLEvent );
        }

        private function openHandler(event:Event):void 
        {
        	//trace("openHandler: " + event);
        }

        private function progressHandler(event:ProgressEvent):void 
        {
        	//trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
        }

        private function securityErrorHandler(event:SecurityErrorEvent):void 
        {
            //trace("securityErrorHandler: " + event);
        }

        private function httpStatusHandler(event:HTTPStatusEvent):void 
        {
            //trace("httpStatusHandler: " + event);
        }

        private function ioErrorHandler(event:IOErrorEvent):void 
        {
            //trace("ioErrorHandler: " + event);
        }
		
	}
}